package common.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.CharArrayWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;

public class FileUtil {
	/**
	 * load unicode text file
	 * @param file filename
	 */
	public static char[] loadFile(String file) throws IOException {
	   BufferedReader reader = null;
	   CharArrayWriter writer = null;
	   UnicodeReader r = new UnicodeReader(new FileInputStream(file), null);
		
	      char[] buffer = new char[16 * 1024];   // 16k buffer
	      int read;
	      try {
	         reader = new BufferedReader(r);
	         writer = new CharArrayWriter();
	         while( (read = reader.read(buffer)) != -1) {
	            writer.write(buffer, 0, read);
	         }
	         writer.flush();
	         return writer.toCharArray();
	      } catch (IOException ex) {
	         throw ex;
	      } finally {
	         try {
	            writer.close(); reader.close(); r.close();
	         } catch (Exception ex) { }
	      }
   	}
	/**
	 * Save text file as UTF-8 with BOM
	 * @param file filename
	 * @param data data to be written
	 * @param append true, append to existing file. false, overwrite file
	 */
	public static void saveFile(String file, String data, boolean append) throws IOException {
		BufferedWriter bw = null;
		OutputStreamWriter osw = null;
		
		File f = new File(file);
		FileOutputStream fos = new FileOutputStream(f, append);
		try {
			// write UTF8 BOM mark if file is empty
			if (f.length() < 1) {
				final byte[] bom = new byte[] { (byte)0xEF, (byte)0xBB, (byte)0xBF };
				fos.write(bom);
			}

			osw = new OutputStreamWriter(fos, "UTF-8");
			bw = new BufferedWriter(osw);
			if (data != null) bw.write(data);
		} catch (IOException ex) {
			throw ex;
		} finally {
			try { bw.close(); fos.close(); } catch (Exception ex) { }
		}
	}
	
	
	
	
	public static String readAsString(String filename) throws IOException{
		File f = new File(filename);
		BufferedReader fin = new BufferedReader(new FileReader(f));
		String content = "";
		String line = "";
		while((line = fin.readLine()) != null){
			content+=line;
		}
		fin.close();
		return content;
	}
	public static String readLines(String filename) throws IOException{
		File f = new File(filename);
		BufferedReader fin = new BufferedReader(new FileReader(f));
		String content = "";
		String line = "";
		while((line = fin.readLine()) != null){
			content+=line+"\n";
		}
		fin.close();
		return content;
	}	
	public static void writeFileText(String filename, String content) throws FileNotFoundException{
		File f = new File(filename);
		PrintWriter fout = new PrintWriter(new FileOutputStream(f));
		fout.print(content);
		fout.close();
	}
	public static int[][] readMatrix(String filename, int column, int row, String regex) throws IOException{
		int[][] matrix = new int[row][column];
		File f = new File(filename);
		BufferedReader fin = new BufferedReader(new FileReader(f));
		int i = 0;
		while(i < row){
			String line = fin.readLine();
			String[] data = line.split(regex);
			for(int j=0;j<column;j++)
				matrix[i][j] = Integer.parseInt(data[j]);
			i++;
		}
		fin.close();
		return matrix;
	}
	
	
	
	public static String[] readListFile(String foldername){
		File folder = new File(foldername);
		ArrayList<String> myList = new ArrayList<String>();
		for(File fileEntry : folder.listFiles()){
			myList.add(fileEntry.getName());
		}
		String[] list = new String[myList.size()];
		list = myList.toArray(list);
		return list;
	}
	public static String[] readListFile(String foldername, String filetype){
		File folder = new File(foldername);
		ArrayList<String> myList = new ArrayList<String>();
		for(File fileEntry : folder.listFiles()){
			if(fileEntry.getName().endsWith(filetype))
					myList.add(fileEntry.getName());
		}
		String[] list = new String[myList.size()];
		list = myList.toArray(list);
		return list;
	}
	
}
